package cc.recent.normal.information.multi_engine;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import cc.recent.normal.information.NativePageActivity;
import io.flutter.embedding.android.FlutterFragmentActivity;
import io.flutter.embedding.android.SplashScreen;
import io.flutter.embedding.engine.FlutterEngine;
import io.flutter.embedding.engine.FlutterEngineCache;

import static cc.recent.normal.information.multi_engine.FlutterLaunchConfigs.EXTRA_CACHED_ENGINE_ID;
import static cc.recent.normal.information.multi_engine.FlutterLaunchConfigs.EXTRA_ENTRYPOINT;
import static cc.recent.normal.information.multi_engine.FlutterLaunchConfigs.EXTRA_INITIAL_ROUTE;

import java.util.Map;

public class SnakeFragmentActivity extends FlutterFragmentActivity implements EngineBindingsDelegate {

    private static final String TAG = "SnakeFragmentActivity";
    private static final int TO_NATIVE_REQUEST_CODE = 10060;
    private static final int TO_FLUTTER_REQUEST_CODE = 10061;
    private EngineBindings mEngineBindings;
    private String engineId = "initialEngine";
    private String entrypoint = "main";
    private String initialRoute = "/";
    @Override
    public void configureFlutterEngine(@NonNull FlutterEngine flutterEngine) {

    }

    public static void open(Context context, String entrypoint, String engineId, String initialRoute) {
        Intent intent = new Intent(context, SnakeFragmentActivity.class);
        intent.putExtra(EXTRA_INITIAL_ROUTE, initialRoute);
        intent.putExtra(EXTRA_ENTRYPOINT, entrypoint);
        intent.putExtra(EXTRA_CACHED_ENGINE_ID,engineId);
        ((Activity)context).startActivityForResult(intent, TO_FLUTTER_REQUEST_CODE);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        init();
        super.onCreate(savedInstanceState);
        Log.d(TAG, "onCreate: ");
        mEngineBindings.attach();
    }

    @Nullable
    @Override
    public FlutterEngine provideFlutterEngine(@NonNull Context context) {
        Log.d(TAG, "provideFlutterEngine: ");
        return mEngineBindings.getEngine();
    }

    private void init(){
        Intent intent = getIntent();
        if (intent != null){
            if (intent.hasExtra(EXTRA_ENTRYPOINT)){
                entrypoint = intent.getStringExtra(EXTRA_ENTRYPOINT);
            }
            if (intent.hasExtra(EXTRA_CACHED_ENGINE_ID)) {
                engineId = intent.getStringExtra(EXTRA_CACHED_ENGINE_ID);
            }
            if (intent.hasExtra(EXTRA_INITIAL_ROUTE)){
                initialRoute = intent.getStringExtra(EXTRA_INITIAL_ROUTE);
            }
        }
        if (mEngineBindings == null)
            mEngineBindings = new EngineBindings(this, this, entrypoint);
        FlutterEngineCache.getInstance().put(engineId, mEngineBindings.getEngine());
    }

    @Nullable
    @Override
    public SplashScreen provideSplashScreen() {
        return new SnakeSplashScreen();
    }

    @Override
    protected void onDestroy() {
        FlutterEngineCache.getInstance().remove(engineId);
        super.onDestroy();
        mEngineBindings.detach();
    }

    @Override
    public void openNativePage(Map<String, Object> args) {
        Intent intent = new Intent(this, NativePageActivity.class);
        SerializableMap wrapperMap = new SerializableMap();
        wrapperMap.setMap(args);
        intent.putExtra("args", wrapperMap);
        startActivityForResult(intent, TO_NATIVE_REQUEST_CODE);
    }

    @Override
    public String getInitialRoute() {
        return initialRoute;
    }


    @Override
    public void pop(Map<String, Object> args) {

        if (args != null && !args.isEmpty()){
            Intent data = new Intent();
            setResult(RESULT_OK, data);
        }
        finish();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == TO_NATIVE_REQUEST_CODE){
            if (data != null){
                Bundle bundle = data.getExtras();
                if (bundle != null){
                    Map<String, Object> result = SnakeUtils.bundleToMap(bundle);
                    mEngineBindings.onNativeResult(result);
                }
            }
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    // flutter 向 native传递参数
}
